package com.approval.approvalapp.myself.activity;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WdqjBean;
import com.approval.approvalapp.myself.adapter.WdqjAdapter;
import com.approval.approvalapp.util.DensityUtil;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

//我的请假
public class WdqjActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.state)
    TextView state;
    @Bind(R.id.state_ll)
    LinearLayout stateLl;
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    //三种数组
    private List<WdqjBean> wdqjBeansAll = new ArrayList<>();
    private List<WdqjBean> wdqjBeansDb = new ArrayList<>();
    private List<WdqjBean> wdqjBeansYb = new ArrayList<>();
    private WdqjAdapter wdqjAdapter;
    @Bind(R.id.empty_ll)
    LinearLayout em;


    @Override
    public int getLayoutId() {
        return R.layout.activity_wdqj;
    }

    @Override
    public void initView() {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(WdqjActivity.this));
        wdqjAdapter = new WdqjAdapter();
        xrv.setAdapter(wdqjAdapter);
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                xrv.setNoMore(true);
            }
        });


    }

    private void refreshData() {
        NetRequsetUtil.getInstance().netRequestGet("v1/center/my_leave", new NetDownResponse() {
            @Override
            public void success(String str) {
                //初始化清空
                wdqjBeansAll.clear();
                wdqjBeansDb.clear();
                wdqjBeansYb.clear();
                //得到全部
                wdqjBeansAll = JSON.parseArray(str, WdqjBean.class);
                //得到代办
                for (int i = 0; i < wdqjBeansAll.size(); i++) {
                    //代办
                    if (wdqjBeansAll.get(i).getIs_have().equals("0")) {
                        wdqjBeansDb.add(wdqjBeansAll.get(i));
                    } else {
                        //已办 1 2
                        wdqjBeansYb.add(wdqjBeansAll.get(i));
                    }
                }

                if (state.getText().toString().equals("全部")){
                    wdqjAdapter.setWdqjBeanList(wdqjBeansAll);
                    if (wdqjBeansAll == null || wdqjBeansAll.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }else if (state.getText().toString().equals("待审批")){
                    wdqjAdapter.setWdqjBeanList(wdqjBeansDb);
                    if (wdqjBeansDb == null || wdqjBeansDb.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }else {
                    wdqjAdapter.setWdqjBeanList(wdqjBeansYb);
                    if (wdqjBeansYb == null || wdqjBeansYb.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }

            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.state_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.state_ll:
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomPopup(this))
                        .show();
                break;
        }
    }

    public class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            final ImageView iv1 = findViewById(R.id.iv1);
            final ImageView iv2 = findViewById(R.id.iv2);
            final ImageView iv3 = findViewById(R.id.iv3);

            if (state.getText().toString().equals("全部")) {
                iv1.setVisibility(VISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else if (state.getText().toString().equals("待审批")) {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(VISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(VISIBLE);
            }


            findViewById(R.id.ll1).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("全部");
                    dismiss();
                    wdqjAdapter.setWdqjBeanList(wdqjBeansAll);
                    if (wdqjBeansAll == null || wdqjBeansAll.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }
            });
            findViewById(R.id.ll2).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("待审批");
                    dismiss();
                    wdqjAdapter.setWdqjBeanList(wdqjBeansDb);
                    if (wdqjBeansDb == null || wdqjBeansDb.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }
            });
            findViewById(R.id.ll3).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("已审批");
                    dismiss();
                    wdqjAdapter.setWdqjBeanList(wdqjBeansYb);
                    if (wdqjBeansYb == null || wdqjBeansYb.size() == 0){
                        em.setVisibility(View.VISIBLE);
                    }else {
                        em.setVisibility(View.GONE);
                    }
                }
            });
        }

        @Override
        protected void onShow() {
            super.onShow();
        }

        @Override
        protected int getMaxWidth() {
            return DensityUtil.dip2px(WdqjActivity.this, 280f);
        }

    }
}
